package cn.falconnect.adserver.adapter.persistence.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import cn.falconnect.adserver.domain.ad.AdMaterialGroup;

@Component
public interface AdMaterialGroupDao {

	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "template_id", property = "adTemplate.id"),
			@Result(column = "material_id", property = "adMaterial.id"),
			@Result(column = "template_spec_id", property = "adTemplateSpec.id"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "interaction_type", property = "interactionType"),
			@Result(column = "interaction_id", property = "interactionId") 
	})
	@Select("select * from ad_material_group")
	List<AdMaterialGroup> findAll();

	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "template_id", property = "adTemplate.id"),
			@Result(column = "material_id", property = "adMaterial.id"),
			@Result(column = "template_spec_id", property = "adTemplateSpec.id"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "interaction_type", property = "interactionType"),
			@Result(column = "interaction_id", property = "interactionId") 
	})
	@Select("select * from ad_material_group where update_time >= #{value}")
	List<AdMaterialGroup> findAfterTime(String warmUpTime);

	
	@Select("select id from ad_material_group where status = 1 and creative_id = #{value}")
	List<Integer> findIdsByCreativeId(long creativeId);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "template_id", property = "adTemplate.id"),
			@Result(column = "material_id", property = "adMaterial.id"),
			@Result(column = "template_spec_id", property = "adTemplateSpec.id"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "interaction_type", property = "interactionType"),
			@Result(column = "interaction_id", property = "interactionId") 
	})
	@Select("select * from ad_material_group where creative_id = #{value}")
	List<AdMaterialGroup> findByCreativeId(long creativeId);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "template_id", property = "adTemplate.id"),
			@Result(column = "material_id", property = "adMaterial.id"),
			@Result(column = "template_spec_id", property = "adTemplateSpec.id"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "interaction_type", property = "interactionType"),
			@Result(column = "interaction_id", property = "interactionId") 
	})
	@Select("select * from ad_material_group where creative_id = #{id} and status = #{status}")
	List<AdMaterialGroup> selectByCreativeId(@Param("id")long id, @Param("status")int status);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "template_id", property = "adTemplate.id"),
			@Result(column = "material_id", property = "adMaterial.id"),
			@Result(column = "template_spec_id", property = "adTemplateSpec.id"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "interaction_type", property = "interactionType") ,
			@Result(column = "interaction_id", property = "interactionId") 
	})
	@Select("select * from ad_material_group where creative_id = #{cid} and template_id = #{tid} and status = #{status}")
	List<AdMaterialGroup> selectByCreativeIdNdTemplateId(@Param("cid")long cid,@Param("tid")long tid, @Param("status")int status);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "template_id", property = "adTemplate.id"),
			@Result(column = "material_id", property = "adMaterial.id"),
			@Result(column = "template_spec_id", property = "adTemplateSpec.id"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "interaction_type", property = "interactionType"),
			@Result(column = "interaction_id", property = "interactionId") 
	})
	@Select("select * from ad_material_group where template_id = #{value}")
	List<AdMaterialGroup> findByTemplateId(long templateId);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "template_id", property = "adTemplate.id"),
			@Result(column = "material_id", property = "adMaterial.id"),
			@Result(column = "template_spec_id", property = "adTemplateSpec.id"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "interaction_type", property = "interactionType"),
			@Result(column = "interaction_id", property = "interactionId") 
	})
	@Select("select * from ad_material_group where template_id = #{id} and status = #{status}")
	List<AdMaterialGroup> selectByTemplateId(@Param("id")long id, @Param("status")int status);
	
}
